// pages/Addaddress/index.js
import areaList from '../../utils/area.js';
import ddt from '../../api/axios.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        areaList,
        name: '',
        phone: '',
        address: '',
        desaddress: '',
        moren: false,
        show: false,
        addresslist: [],
        defaultadd: 2,
        userinfo: '',
        id: '',
    },
    showtrue() {
        this.setData({
            show: true
        })
    },
    commit() {
        var llist = [
            this.data.userinfo.user_id,
            this.data.name,
            this.data.phone,
            this.data.addresslist[0],
            this.data.desaddress,
        ]
        if (llist.includes(undefined) || llist.includes('')) {
            wx.showToast({
                title: '请填写信息',
                icon: 'error'
            })
            return
        }
        console.log(this.data.addresslist[2]);
        ddt('post', '/api/user/editaddress', {
            addressid: this.data.id,
            user_name: this.data.name,
            phone: this.data.phone,
            province: this.data.addresslist[0].name,
            city: this.data.addresslist[1].name,
            district: this.data.addresslist[2].name,
            address: this.data.desaddress,
            defaultadd: this.data.defaultadd,
            code:this.data.addresslist[2].code
        }).then(res => {
            if (res.data.code == 1) {
                wx.showToast({
                    title: '修改成功',
                    icon: 'success'
                })
                wx.navigateBack({})
            } else {
                console.log(res);
                wx.showToast({
                    
                    title: res.data.msg,
                    icon: 'error'
                })
            }
        })
    },
    onChange(e) {
        this.setData({
            name: e.detail
        })
    },
    onChange1(e) {
        this.setData({
            phone: e.detail
        })
    },
    onChange2(e) {
        this.setData({
            desaddress: e.detail.value
        })
    },
    confirm(e) {
        console.log(e)
        var aa = e.detail.values[0].name + '-' + e.detail.values[1].name + '-' + e.detail.values[2].name
        this.setData({
            addresslist: e.detail.values,
            address: aa,
            show: false
        })
    },
    onClose() {
        this.setData({
            show: false
        })
    },
    radio(event) {
        if (event.detail == true) {
            this.setData({
                defaultadd: 1
            })
        } else {
            this.setData({
                defaultadd: 2
            })
        }
        this.setData({
            moren: event.detail,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            areaList
        })
        console.log('bbb',options.data)
        console.log(
            
            JSON.parse(options.data)
        )
        var data = JSON.parse(options.data)
        var add = [{
            name: data.province 
        },
        {
            name: data.city
        },
        {
            name: data.district,
            code:data.areaCode
        }
    ]
        this.setData({
            id: data.id,
            name: data.username,
            phone: data.phone,
            addresslist: add,
            address: data.province + '-' + data.city + '-' + data.district,
            desaddress: data.address,
            moren:data.defaultadd==2?false:true
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            userinfo: wx.getStorageSync('userinfo')
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})